﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Net;

using EnvironmentalMonitor.Support.Device.Handler;
using EnvironmentalMonitor.Support.Device;
using EnvironmentalMonitor.Support.Instruction.In.Request;
using EnvironmentalMonitor.Support.Instruction.In.Response;
using EnvironmentalMonitor.Support.Instruction.Out.Request;
using EnvironmentalMonitor.Support.Instruction.Out.Response;
using EnvironmentalMonitor.Support.Instruction;
using EnvironmentalMonitor.Support.Business.Environmental;
using EnvironmentalMonitor.Support.Business;
using EnvironmentalMonitor.Support.Module;
using EnvironmentalMonitor.Support.Resource;

namespace EnvironmentalMonitor.WebApplication.Environmental.Machine.Setup
{
    public partial class IpSaveJson : EnvironmentalMonitor.WebApplication.Abstract.AbstractSetupSaveJson
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            const string GUID = "guid";

            const string IP = "ip";
            const string IP_CACHE = "ipCache";
            const string NETMASK_CACHE = "netmaskCache";
            const string GATEWAY_CACHE = "gatewayCache";

            EnvironmentalMonitor.Support.Module.Manage.User sessionUser = this.Session[Constant.SESSION_KEY_USER] as EnvironmentalMonitor.Support.Module.Manage.User;
            DateTime now = DateTime.Now;

            EnvironmentalMonitor.Support.Module.Environmental.Machine module = new Support.Module.Environmental.Machine();
            module.Guid = this.ParameterString(GUID);
            module.UpdateUserId = sessionUser.Guid;
            module.UpdateTime = now;

            module.Ip = this.ParameterString(IP);
            module.IpCache = this.ParameterString(IP_CACHE);
            module.NetmaskCache = this.ParameterString(NETMASK_CACHE);
            module.GatewayCache = this.ParameterString(GATEWAY_CACHE);

            IPAddress ip = IPAddress.Any;

            bool ipRight = IPAddress.TryParse(module.Ip, out ip);
            bool ipCacheRight = IPAddress.TryParse(module.IpCache, out ip);
            bool netmaskCacheRight = IPAddress.TryParse(module.NetmaskCache, out ip);
            bool gatewayCacheRight = IPAddress.TryParse(module.GatewayCache, out ip);

            this.SaveJsonData = new Abstract.SaveJsonData();
            if (ipRight && ipCacheRight && netmaskCacheRight && gatewayCacheRight)
            {
                this.SaveJsonData.success = false;
                this.SaveJsonData.msg = "设置操作错误！";

                MachineBusiness business = new MachineBusiness();
                business.UpdateIpCache(module);

                IpInstruction instruction = new IpInstruction(module.IpCache, module.NetmaskCache, module.GatewayCache);
                byte[] values = instruction.Encode();
                Terminal.Run(module.Ip, values);

                Nullable<bool> result = this.QueryTask(module.Ip, typeof(IpSuccessInstruction).FullName, typeof(IpErrorInstruction).FullName, now);

                if (result != null)
                {
                    this.SaveJsonData.success = result.Value;
                    if (result.Value)
                    {
                        bool done = business.UpdateIp(module);
                        if (done)
                        {
                            this.SaveJsonData.msg = "设置操作成功！更新缓存成功！";
                        }
                        else
                        {
                            this.SaveJsonData.msg = "设置操作成功！更新缓存失败！";
                        }
                    }
                    else
                    {
                        this.SaveJsonData.msg = "设置操作失败！";
                    }
                }
            }
            else
            {
                this.SaveJsonData.success = false;
                this.SaveJsonData.msg = "设置操作参数无效！";
            }

            string json = this.Json();

            this.Response.Write(json);
            this.Response.Flush();
            this.Response.End();
        }
    }
}